package com.zmn.plat.admin.controller.product.tariff.tariffproductitem3;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.model.entity.product.tariff.tariffproductitem3.TariffProductItem3;
import com.zmn.plat.services.interfaces.product.tariff.tariffproductitem3.TariffProductItem3Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 价格表-产品-项目-计价器3.0基础修改Controller
 *
 * @author guoqiao
 * @since 2021/11/19 15:28
 */
@Slf4j
@RestController
@RequestMapping("/product/tariff/tariffproductitem3")
public class TariffProductItem3ModifyController extends ProductTariffGroupItemBase3Controller {
    @Resource
    private TariffProductItem3Service tariffProductItem3Service;

    /**
     * 描述: 保存
     *
     * @author guoqiao
     * @since 2021/11/19 15:28
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody TariffProductItem3 tariffProductItem3) {

        Date now = DateUtil.getNow();
        try {
            /**
             * 将单位元转换成分
             */
            tariffProductItem3.setHourFee(tariffProductItem3.getHourFee() * 100);
            tariffProductItem3.setTrafficFee(tariffProductItem3.getTrafficFee() * 100);
            tariffProductItem3.setPartFee(tariffProductItem3.getPartFee() * 100);


            if (NumberUtil.isNotNullOrZero(tariffProductItem3.getInternalSettlementPrice())) {
                tariffProductItem3.setInternalSettlementPrice(tariffProductItem3.getInternalSettlementPrice() * 100);
            }
            if (NumberUtil.isNotNullOrZero(tariffProductItem3.getExternalSettlementPrice())) {
                tariffProductItem3.setExternalSettlementPrice(tariffProductItem3.getExternalSettlementPrice() * 100);
            }

            if (NumberUtil.isNullOrZero(tariffProductItem3.getRelateId())) {
                tariffProductItem3.setCreater(getMcStaffRealName());
                tariffProductItem3.setCreateTime(now);
                tariffProductItem3Service.insert(tariffProductItem3);
            } else {
                tariffProductItem3.setUpdater(getMcStaffRealName());
                tariffProductItem3.setUpdateTime(now);
                tariffProductItem3Service.updateByKey(tariffProductItem3);
            }
            return ResponseDTO.success(tariffProductItem3.getRelateId(), "操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 描述: 删除
     *
     * @param relateId 主键
     * @author guoqiao
     * @since 2021/11/19 15:28
     */
    @RequestMapping(value = "/delete")
    public ResponseDTO delete(@RequestParam(value = "relateId") Integer relateId) {

        try {
            if (NumberUtil.isNullOrZero(relateId)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            tariffProductItem3Service.deleteByKey(relateId);
            return ResponseDTO.success("删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

}
